package DFS;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-17
 * Time:12:28
 * Text：129. 求根节点到叶节点数字之和
 */


public class Text1 {
    public int dfs(TreeNode root,int preSum){
        preSum=preSum*10+root.val;
        if(root.left==null && root.right== null){
            return preSum;
        }
        int ret=0;
        if(root.left!=null) ret+=dfs(root.left,preSum);
        if(root.right!=null) ret+=dfs(root.right,preSum);
        return ret;
    }
    public int sumNumbers(TreeNode root) {
        return dfs(root,0);
    }
}
